package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.entity.SmartReservation;
import com.ruoyi.system.domain.generation.IncreaseMonthlyCardInfoRequest;
import com.ruoyi.system.domain.generation.IncreaseWhileListInfoRequest;
import com.ruoyi.system.domain.generation.UpdateMonthlyCardInfoRequest;
import com.ruoyi.system.domain.generation.UpdateWhileListInfoRequest;
import com.ruoyi.system.domain.moneyentity.SmartCarMonthlyCardRelations;
import com.ruoyi.system.domain.moneyentity.SmartWhitelist;
import com.ruoyi.system.service.ISmartSystemConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * SmartSystemConfigurationController
 * 系统设置controller
 *
 * @author life.
 * @date 2025/3/21
 */
@RestController
@RequestMapping("/smart")
public class SmartSystemConfigurationController extends BaseController {
    @Autowired
    private ISmartSystemConfigurationService iSmartSystemConfigurationService;



    /**
     * 系统管理接口
     */
    /**
     * 月卡规则查询
     */
    @RequiresPermissions("system:smart:query")
    @Log(title = "月卡规则查询", businessType = BusinessType.INSERT)
    @GetMapping("/getmonthcards")
    public TableDataInfo getmonthcards(@RequestParam(required = false) Long scBindingId,
                                       @RequestParam(required = false) Date scActivationDate,
                                       @RequestParam(required = false) Date scExpiryDate,
                                       @RequestParam(required = false) Long scIsActive) {
        startPage();
        List<SmartCarMonthlyCardRelations> smartCarMonthlyCardRelations = iSmartSystemConfigurationService.selectAllSmartCarMonthlyCardRelations(scBindingId,scActivationDate,scExpiryDate,scIsActive);
        System.out.println(scIsActive);

        return getDataTable(smartCarMonthlyCardRelations);
    }


    /**
     * 上下架指定月卡规则
     */
    @RequiresPermissions("system:smart:disable")
    @Log(title = "上下架指定月卡规则", businessType = BusinessType.INSERT)
    @GetMapping("/getmonthupcards")
    public AjaxResult enableSmartChargingRules(@RequestParam("scMonthCardId") Long scMonthCardId,@RequestParam("isactive") Long scIsActive) {
        if (StringUtils.isNull(scMonthCardId)) {
            return error("月卡绑定 ID 不能为空");
        }
        int rows = iSmartSystemConfigurationService.updateScIsActiveByMonthlyCardBindingId(scMonthCardId,scIsActive);
        if (rows > 0) {
            return success("上架成功");
        } else {
            return error("上架失败，请检查月卡绑定 ID 是否正确");
        }
    }


    /**
     * 修改月卡信息及关联的规则信息
     */
    @RequiresPermissions("system:smart:update")
    @Log(title = "修改月卡信息及关联的规则信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updateInfo")
    public AjaxResult updateMonthlyCardInfo(@RequestBody UpdateMonthlyCardInfoRequest request) {
        if (request.getScBindingId() <= 0) {
            return error("绑定 ID 不合法");
        }
        int rows = iSmartSystemConfigurationService.updateMonthlyCardInfo(request);
        if (rows > 0) {
            return success("月卡信息及关联的规则信息修改成功");
        } else {
            return error("月卡信息及关联的规则信息修改失败，请检查绑定 ID 是否正确");
        }
    }


    /**
     * 新增月卡信息
     */
    @RequiresPermissions("system:smart:add")
    @Log(title = "修改月卡信息及关联的规则信息", businessType = BusinessType.UPDATE)
    @PostMapping("/monthadd")
    public AjaxResult addMonthlyCardInfo(@RequestBody IncreaseMonthlyCardInfoRequest request) {
        if (request.getScBindingId() <= 0) {
            return error("绑定 ID 不合法");
        }
        int rows = iSmartSystemConfigurationService.insertMonthlyCardInfo(request);
        if (rows > 0) {
            return success("月卡信息及关联充电规则信息新增成功");
        } else {
            return error("月卡信息及关联充电规则信息新增失败，请检查数据是否正确");
        }
    }

    /**
     * 会员规则查询
     */
    @RequiresPermissions("system:while:list")
    @Log(title = "会员规则查询", businessType = BusinessType.INSERT)
    @GetMapping("/getwhilelistcards")
    public TableDataInfo getwhilelistcards(@RequestParam(required = false) Long scWhitelistId,
                                           @RequestParam(required = false)Date scValidPeriodStart,
                                           @RequestParam(required = false)Date scValidPeriodEnd,
                                           @RequestParam(required = false)Long scIsActive) {
        startPage();
        List<SmartWhitelist> smartWhitelists = iSmartSystemConfigurationService.selectAllSmartWhitelist(scWhitelistId,scValidPeriodStart,scValidPeriodEnd,scIsActive);
        return getDataTable(smartWhitelists);
    }

    /**
     * 修改会员上下架状态
     */
    @RequiresPermissions("system:smart:disable")
    @Log(title = "修改会员上下架状态", businessType = BusinessType.UPDATE)
    @GetMapping("/updatewhilelistcards")
    public AjaxResult disableSmartChargingRules(@RequestParam("scWhitelistId") Long scWhitelistId,@RequestParam("isactive") Long scIsActive) {
        if (StringUtils.isNull(scWhitelistId)) {
            return error("白名单 ID 不能为空");
        }
        int rows = iSmartSystemConfigurationService.updateScIsActiveByWhitelistId(scWhitelistId,scIsActive);
        if (rows > 0) {
            return success("下架成功");
        } else {
            return error("下架失败，请检查白名单 ID 是否正确");
        }
    }

    /**
     * 修改会员信息及关联的规则信息
     */
    @RequiresPermissions("system:smart:update")
    @Log(title = "修改会员信息及关联的规则信息", businessType = BusinessType.UPDATE)
    @PostMapping("/updatewhilelistInfo")
    public AjaxResult updatewhilelistInfo(@RequestBody UpdateWhileListInfoRequest request) {
        if (request.getScWhitelistId() <= 0) {
            return error("绑定 ID 不合法");
        }
        int rows = iSmartSystemConfigurationService.updateMemberInfo(request);
        if (rows > 0) {
            return success("会员信息及关联的规则信息修改成功");
        } else {
            return error("会员信息及关联的规则信息修改失败，请检查绑定 ID 是否正确");
        }
    }
    /**
     * 新增会员信息
     */
    @RequiresPermissions("system:smart:add")
    @Log(title = "新增会员信息", businessType = BusinessType.INSERT)

    @PostMapping("/whileadd")
    public AjaxResult addMemberInfo(@RequestBody IncreaseWhileListInfoRequest request) {
        if (request.getScWhitelistId() == null) {
            return error("会员 ID 不能为空");
        }
        int rows = iSmartSystemConfigurationService.insertMemberInfo(request);
        if (rows > 0) {
            return success("会员信息信息新增成功");
        } else {
            return error("会员信息规则信息新增失败，请检查数据是否正确");
        }
    }



}